#include "gifplaintextextension.h"
#include <QDataStream>

namespace WingGif {

int GifPlainTextExtension::blockSize() { return _blockSize; }

int GifPlainTextExtension::left() { return _left; }

int GifPlainTextExtension::top() { return _top; }

int GifPlainTextExtension::width() { return _width; }

int GifPlainTextExtension::height() { return _height; }

int GifPlainTextExtension::cellWidth() { return _cellWidth; }

int GifPlainTextExtension::cellHeight() { return _cellHeight; }

int GifPlainTextExtension::foregroundColorIndex() {
  return _foregroundColorIndex;
}

int GifPlainTextExtension::backgroundColorIndex() {
  return _backgroundColorIndex;
}

QString GifPlainTextExtension::text() { return _text; }

QList<GifExtension *> GifPlainTextExtension::extensions() {
  return _extensions;
}

bool GifPlainTextExtension::read(QIODevice *stream,
                                 QList<GifExtension *> controlExtensions) {
  // Note: at this point, the label (0x01) has already been read
  auto bytes = stream->read(13);
  _blockSize = bytes[0];

  if (_blockSize != 12)
    return false;

  auto pbyte = bytes.data();
  _left = *reinterpret_cast<short *>(pbyte + 1);
  _top = *reinterpret_cast<short *>(pbyte + 3);
  _width = *reinterpret_cast<short *>(pbyte + 5);
  _height = *reinterpret_cast<short *>(pbyte + 7);
  _cellWidth = bytes[9];
  _cellHeight = bytes[10];
  _foregroundColorIndex = bytes[11];
  _backgroundColorIndex = bytes[12];

  char len;
  stream->read(&len, 1);
  _text = stream->read(len);
  _extensions = controlExtensions;
  return true;
}

GifBlockKind GifPlainTextExtension::kind() {
  return GifBlockKind::GraphicRendering;
}

GifPlainTextExtension::GifPlainTextExtension(QObject *parent)
    : GifExtension(parent) {}

} // namespace WingGif
